﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockCommon.data;
using StockCommon.baseclass;
using Utilities;
using StockCommon;
using StockImport.dataimport.common;
using HAP = HtmlAgilityPack;
using NetLib.web;
using System.IO;
using System.Threading;

namespace StockImport.dataimport.tse.sb
{
    //URL :http://bsr.twse.com.tw/bshtm/
    public class TSE_SB_Fetcher :SB_Fetcher_Common
    {
  
        protected override TwnStockMarketType getTargetMarket()
        {
            return TwnStockMarketType.上市;
        }

        protected override String getStoreFileName(String code, DateTime date)
        {
            String pat = @"D:\SBData\temptse\{0}\{1}.html";
            return String.Format(pat, DateUtils.toShortDateFormat_NoSlash(date), code);
        }
        protected override bool CrawlingSB(string code, DateTime date, int tc)
        {
            String fileName = getStoreFileName(code, date);
            if(File.Exists(fileName))
                return true;
            int pageCount = 0;
            try
            {
                pageCount = tryDetectPageCount(code, tc);
            }
            catch(Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
                return false;
            }
            if (pageCount < 0)
                return false;
            System.Diagnostics.Debug.WriteLine("代號:" + code + " 筆數 " + tc + "=>頁數" + pageCount);
            String url = getURL(code, pageCount);
            bool success;
            String opMessage = "";
            //String content = HttpGet.process(url, out success, out opMessage);
            String content = HttpGet.downloadPage(url, out success, out opMessage);
            if(success==false)
            {
                Thread.Sleep(2000);
                if (opMessage.Contains("The underlying connection was closed: An unexpected error occurred on a receive"))
                    content = HttpGet.downloadPage(url, out success, out opMessage);
            }
            
            if (success)
            {
                //必須要過濾 
                if (hasValidRecord(content))
                {
                    FileUtils.saveUTF8(fileName, content);
                    return true;
                }
            }
            return false;
        }
        protected String getURL(String code, int count)
        {
            String s = String.Format("http://bsr.twse.com.tw/bshtm/bsContent.aspx?StartNumber={0}&FocusIndex=All_{1}&flg_Print=1", code, count);
            return s;
        }
        //這個是..根據市場
        private int tryDetectPageCount(string code, int tradeCount)
        {
            //get
            int max = tradeCount / 100 + 1;
            if (isValidPage(singleURL(code, max)))
                return max;

            int step = 3;
            if (max > 30)
                step = max / 10;

            int cur = max;

            int r = detectTotalPage(code, 1, max);
            
            return r;
        }

        protected int detectTotalPage(String code, int min, int max)
        {
            int mid = (min + max) / 2;
            if (mid == min)
                return mid;
            if (isValidPage(singleURL(code, mid)))
            {
                return detectTotalPage(code, mid, max);
            }
            else
            {
                return detectTotalPage(code, min, mid);
            }
        }
        protected bool hasValidRecord(String content)
        {
            int cz = 0;
            HAP.HtmlDocument doc = new HAP.HtmlDocument();

            doc.LoadHtml(content);

            HAP.HtmlNodeCollection price_3s = doc.DocumentNode.SelectNodes("//tr[@class='column_value_price_3']");
            HAP.HtmlNodeCollection price_2s = doc.DocumentNode.SelectNodes("//tr[@class='column_value_price_2']");

            if (price_2s != null)
            {
                cz += price_2s.Count;
            }
            if (price_3s != null)
            {
                cz += price_3s.Count;
            }

            if (cz > 0)
                return true;
            return false;

        }
        protected bool isValidPage(String url)
        {
            bool success;
            String message;
            String content = HttpGet.downloadPage(url, out success, out message);
            if (success == false)
            {
                if (message.Contains("The underlying connection was closed: An unexpected error occurred on a receive"))
                    throw new Exception("Connection Closed Need Retry");
                return false;
            }
            return hasValidRecord(content);                         
        }

        protected string singleURL(String code, int pageCount)
        {
            String r = String.Format("http://bsr.twse.com.tw/bshtm/bsContent.aspx?StartNumber={0}&FocusIndex={1}&flg_Print=0", code, pageCount);
            return r;
        }




    }
}
